package org.leviatan.dataharbour.gui.swingcomponents;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.JTree;

import org.leviatan.dataharbour.gui.RuntimeContext;
import org.leviatan.dataharbour.gui.listeners.RequestTreeListener;
import org.leviatan.dataharbour.gui.swingcomponents.views.HarbourRequestDetailView;
import org.leviatan.dataharbour.gui.swingmodels.TreeMdlSender;

public class UnpersistedRequestsTab extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7506166273077306244L;

	private JSplitPane jSplitPane;
	
	private JScrollPane jScrollPaneLeft;
	
	private JScrollPane jScrollPaneRight;

	/**
	 * This is the default constructor
	 */
	public UnpersistedRequestsTab() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setLayout(new BorderLayout());
		this.add(getJSplitPane());
		
		this.requestsTree.addTreeSelectionListener(
				new RequestTreeListener(requestsTree));
	}
	
	private JSplitPane getJSplitPane() {
		if (jSplitPane == null) {
			jSplitPane = new JSplitPane();
			jSplitPane.setLeftComponent(getLeftJScrollPane());
			jSplitPane.setRightComponent(getRightJScrollPane());
		}
		return jSplitPane;
	}
	
	private JScrollPane getLeftJScrollPane() {
		if (jScrollPaneLeft == null) {
			jScrollPaneLeft = new JScrollPane(getLeftPanel());
			jScrollPaneLeft.setPreferredSize(new Dimension(300, 500));
		}
		return jScrollPaneLeft;
	}
	
	
	private JScrollPane getRightJScrollPane() {
		if (jScrollPaneRight == null) {
			jScrollPaneRight = new JScrollPane(new HarbourRequestDetailView());
			//jScrollPaneRight.add(new RightEditViewPane());
			//jScrollPaneRight.add(getJTable());
			//jScrollPaneRight.setPreferredSize(new Dimension(300, 0));
		}
		return jScrollPaneRight;
	}
	
	private JToolBar tBar;
	
	private JTree requestsTree;
	
	private JPanel leftPanel;
	
	private JButton butCreateTbl, butDelete, butCreateFld, butCreateRec;
	
	public JPanel getLeftPanel() {
	
		if (leftPanel == null) {
			leftPanel = new JPanel();
		}
		leftPanel.setLayout(new GridBagLayout());
		
		//this.setPreferredSize(size);
		
		this.requestsTree = this.initJTree();
		RuntimeContext.requestsTree = this.requestsTree;
		
		GridBagConstraints tBarConstr = new GridBagConstraints();
		tBarConstr.anchor = GridBagConstraints.NORTHWEST;
		tBarConstr.fill = GridBagConstraints.BOTH;
		tBarConstr.gridx = 0;
		tBarConstr.gridy = 0;
		
		GridBagConstraints treeConstr = new GridBagConstraints();
		treeConstr.anchor = GridBagConstraints.NORTHWEST;
		treeConstr.insets = new Insets(5, 5, 5, 5);
		treeConstr.fill = GridBagConstraints.BOTH;
		treeConstr.gridx = 0;
		treeConstr.gridy = 1;
		treeConstr.weightx = 1.0;
		treeConstr.weighty = 1.0;
		
		leftPanel.add(this.requestsTree, treeConstr);
		
		
		// add listeners to buttons
			
		return leftPanel;
	}
	
	private JTree initJTree() {
		if (requestsTree == null ) {
			requestsTree = new JTree();
			requestsTree.setModel(new TreeMdlSender());
			//ListenerFactory.initTreeSelectionListener(requestsTree);
			//jTree.setPreferredSize(size);
		}
		return requestsTree;
	}
	
	public JToolBar initToolBar() {
		
		JToolBar aBar = new JToolBar("tBar");
		aBar.setLayout(new GridBagLayout());
		
		GridBagConstraints butCreateTblConstr = new GridBagConstraints();
		butCreateTblConstr.anchor = GridBagConstraints.WEST;
		butCreateTblConstr.insets = new Insets(0, 5, 0, 5);
		butCreateTblConstr.gridx = 0;
		butCreateTblConstr.gridy = 0;
		butCreateTblConstr.fill = GridBagConstraints.BOTH;
	
		GridBagConstraints butDeleteConstr = new GridBagConstraints();
		butDeleteConstr.anchor = GridBagConstraints.WEST;
		butDeleteConstr.insets = new Insets(0, 0, 0, 5);
		butDeleteConstr.gridx = 3;
		butDeleteConstr.gridy = 0;
		butDeleteConstr.weightx = 1.0;
		butDeleteConstr.weighty = 1.0;
		
		GridBagConstraints butCreateRecConstr = new GridBagConstraints();
		butCreateRecConstr.anchor = GridBagConstraints.WEST;
		butCreateRecConstr.insets = new Insets(0, 0, 0, 5);
		butCreateRecConstr.gridx = 1;
		butCreateRecConstr.gridy = 0;
		
		GridBagConstraints butCreateFldConstr = new GridBagConstraints();
		butCreateFldConstr.anchor = GridBagConstraints.WEST;
		butCreateFldConstr.insets = new Insets(0, 0, 0, 5);
		butCreateFldConstr.gridx = 2;
		butCreateFldConstr.gridy = 0;
		
		butCreateTbl = new JButton("new table");
		butDelete = new JButton("delete");
		butCreateFld = new JButton("new field");
		butCreateRec = new JButton("new record");
		
		aBar.add(butCreateTbl, butCreateTblConstr);
		aBar.add(butDelete, butDeleteConstr);
		aBar.add(butCreateFld, butCreateFldConstr);
		aBar.add(butCreateRec, butCreateRecConstr);
		
		aBar.setRollover(true);
		aBar.setFloatable(false);
		
		return aBar;
		
	}

}
